package test;

import com.cabolabs.xre.core.definitions.RuleDefinition
import com.cabolabs.xre.core.execution.Executer;
import com.cabolabs.xre.engine.parser.RuleDefinitionParser;
import com.thoughtworks.xstream.XStream
import groovy.util.GroovyTestCase


class Rule2Test extends GroovyTestCase {

   // Sin static no se mantienen los valores entre tests
   static RuleDefinition rule
   static String sessionId
   
   
   public void testParse()
   {
      long start = System.currentTimeMillis()
      // -------------------------------------------------------
      
      
      def path = 'rules\\rule2_simple_if_and.xrl.xml'
      rule = RuleDefinitionParser.parse(new File(path))
      
      
      // -------------------------------------------------------
      long now = System.currentTimeMillis()
      println "read from file and parse time: " + (now - start) + " ms"
      
      
      XStream xstream = new XStream()
      println xstream.toXML(rule)
   }
   
   public void testAdd()
   {
      long start = System.currentTimeMillis()
      // -------------------------------------------------------
      
      
      def executer = Executer.getInstance()
      
      sessionId = executer.add(rule)
      
      
      // -------------------------------------------------------
      long now = System.currentTimeMillis()
      println "executer add time: " + (now - start) + " ms"
      
      
      assert executer.contains(sessionId)
      
      assert executer.getExecutionIds().contains(sessionId)
   }
   
   public void testInit()
   {
      long start = System.currentTimeMillis()
      // -------------------------------------------------------

            
      def executer = Executer.getInstance()
      
      def isOk = executer.init(sessionId, null) // OJO: params null
      

      // -------------------------------------------------------
      long now = System.currentTimeMillis()
      println "executer init time: " + (now - start) + " ms"
      
      
      assert isOk
   }
   
   public void testExecute()
   {
      long start = System.currentTimeMillis()
      // -------------------------------------------------------

      
      def executer = Executer.getInstance()
      
      def isOk = executer.execute(sessionId, null) // OJO: input es null
      
      
      // -------------------------------------------------------
      long now = System.currentTimeMillis()
      println "executer execute time: " + (now - start) + " ms"
      
      
      assert isOk
   }
   
   public void testResults()
   {
      long start = System.currentTimeMillis()
      // -------------------------------------------------------

      
      def executer = Executer.getInstance()
      
      def instances = executer.getResults(sessionId)
      
      instances.each { varins ->
         
         println varins.declaration.name +"="+ varins.value +"("+ varins.declaration.type +")"
      }
      
      // -------------------------------------------------------
      long now = System.currentTimeMillis()
      println "executer results time: " + (now - start) + " ms"
   }
   
   public void testErrors()
   {
      def executer = Executer.getInstance()
      def excepts = executer.getErrors(sessionId)
      
      excepts.each { e ->
         
         e.printStackTrace()
      }
   }
}